#include <stdio.h>
#include <pthread.h>

#define NLOOP 5000


int var = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void *idat(void *);

int main(void)
{
    pthread_t tidA, tidB;
    pthread_create(&tidA, NULL, idat, "ThreadA");
    pthread_create(&tidB, NULL, idat, "ThreadB");
    pthread_join(tidA, NULL);
    pthread_join(tidB, NULL);
    return 0;
}

void *idat(void *arg) {
    pthread_t tid = pthread_self();
    int counter = 0, tvar;
    while(counter++ < NLOOP){
        pthread_mutex_lock(&mutex);
        tvar = var;
        printf("%s,tid:%u,counter:%d\n", arg, tid, var);
        var = tvar + 1; 
        pthread_mutex_unlock(&mutex);
    }
    return NULL;
}
